package org.dromara.system.controller.device;

import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.dromara.system.domain.bo.VariableAssociationGroupBo;
import org.dromara.system.domain.vo.VariableAssociationGroupVo;
import org.dromara.system.service.IVariableAssociationGroupService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 变量分类
 *
 * @author Lion Li
 * @date 2024-10-21
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/associationGroup")
public class VariableAssociationGroupController extends BaseController {

    private final IVariableAssociationGroupService variableAssociationGroupService;

    /**
     * 查询变量分类列表
     */
    @SaCheckPermission("system:association:list")
    @GetMapping("/list")
    public TableDataInfo<VariableAssociationGroupVo> list(VariableAssociationGroupBo bo, PageQuery pageQuery) {
        return variableAssociationGroupService.queryPageList(bo, pageQuery);
    }


    /**
     * 获取变量分类详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("system:association:list")
    @GetMapping("/{id}")
    public R<VariableAssociationGroupVo> getInfo(@NotNull(message = "主键不能为空")
                                                 @PathVariable Long id) {
        return R.ok(variableAssociationGroupService.queryById(id));
    }

    /**
     * 新增变量分类
     */
    @SaCheckPermission("system:association:list")
    @Log(title = "变量分类", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody VariableAssociationGroupBo bo) {
        return toAjax(variableAssociationGroupService.insertByBo(bo));
    }

    /**
     * 修改变量分类
     */
    @SaCheckPermission("system:association:list")
    @Log(title = "变量分类", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody VariableAssociationGroupBo bo) {
        return toAjax(variableAssociationGroupService.updateByBo(bo));
    }

    /**
     * 删除变量分类
     *
     * @param ids 主键串
     */
    @SaCheckPermission("system:association:list")
    @Log(title = "变量分类", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(variableAssociationGroupService.deleteWithValidByIds(List.of(ids), true));
    }
}
